package com.example.ymm.modules1.service.impl.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.sys.SysApprovalSetting;
import com.example.ymm.modules1.entity.my.user.DbApprovalLog;
import com.example.ymm.modules1.entity.my.user.DbUserWait;
import com.example.ymm.modules1.mapper.my.user.DbUserWaitMapper;
import com.example.ymm.modules1.query.DbUserWaitQueryCriteria;
import com.example.ymm.modules1.query.sys.SysApprovalSettingQueryCriteria;
import com.example.ymm.modules1.service.dd.ms.DingWaitService;
import com.example.ymm.modules1.service.sys.SysApprovalSettingService;
import com.example.ymm.modules1.service.user.DbApprovalLogService;
import com.example.ymm.modules1.service.user.DbUserWaitService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class DbUserWaitServiceImpl extends BaseServiceImpl<DbUserWaitMapper, DbUserWait> implements DbUserWaitService {

    @Autowired
    DbApprovalLogService dbApprovalLogService;

    @Autowired
    SysApprovalSettingService sysApprovalSettingService;
    @Autowired
    DingWaitService dingWaitService;

    @Override
    public TableList<DbUserWait> list(DbUserWaitQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbUserWait> page = new Page<DbUserWait>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbUserWait.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbUserWait> tableList=new TableList(page);
        return tableList;
    }

    public List<DbUserWait> queryAll(DbUserWaitQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbUserWait.class, criteria);
        predicate.orderByDesc("create_time");
        List<DbUserWait> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public DbUserWait findByApproveNo(String approveNo,String userId) {
        DbUserWaitQueryCriteria criteria=new DbUserWaitQueryCriteria();
        criteria.setApproveNo(approveNo);
        criteria.setUserId(userId);
        List<DbUserWait> dbUserWaits = queryAll(criteria);
        if(dbUserWaits.size()>0){
            return dbUserWaits.get(0);
        }
        return null;
    }

    @Override
    public DbUserWait findByApprovalNodeId(String approvalNodeId) {
        DbUserWaitQueryCriteria criteria=new DbUserWaitQueryCriteria();
        criteria.setApprovalNodeId(approvalNodeId);
        List<DbUserWait> dbUserWaits = queryAll(criteria);
        if(dbUserWaits.size()>0){
            return dbUserWaits.get(0);
        }
        return null;
    }

    @Override
    public List<JSONObject> myWaitList(DbUserWaitQueryCriteria criteria, SysApprovalSettingQueryCriteria c2) {
        List<JSONObject> list=new ArrayList<JSONObject>();

        List<SysApprovalSetting> settings = sysApprovalSettingService.queryAll(c2);
        for(SysApprovalSetting setting:settings){
            LambdaQueryWrapper<DbApprovalLog> query = Wrappers.lambdaQuery();
            query.eq(DbApprovalLog::getCurrentUserId,criteria.getUserId());
            query.eq(DbApprovalLog::getStatus,1);
            query.eq(DbApprovalLog::getApprovalSettingId,setting.getApprovalSettingId());
            long count = dbApprovalLogService.count(query);
            if(count>0){
                JSONObject j1=new JSONObject();
                j1.put("title",setting.getApprovalName());
                j1.put("color","orange");
                j1.put("page",setting.getPage());
                j1.put("value",count);
                j1.put("approvalSettingId",setting.getApprovalSettingId());
                list.add(j1);
            }
        }

        return list;
    }

    @Override
    public void completeUserWait(String approvalNodeId) {
        DbUserWait userWait = findByApprovalNodeId(approvalNodeId);
        if(UtilValidate.isNotEmpty(userWait)){
            userWait.setStatus(1);
            saveOrUpdate(userWait);
            dingWaitService.finishTodo(userWait);
        }
    }
}
